package cc.magicjson.rest.mybatis;


import cc.magicjson.entity.MyBatisUser;
import cc.magicjson.entity.User;
import cc.magicjson.entity.User;
import cc.magicjson.service.mybatis.UserServiceMyBatis;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mybatis/users")
public class UserControllerMyBatis {

    private final UserServiceMyBatis userService;

    public UserControllerMyBatis(UserServiceMyBatis userService) {
        this.userService = userService;
    }

       @PostMapping("/MybatisSaveAndFlush")
    public User saveAndFlush(MyBatisUser user) {
        return userService.saveAndFlush(user);
    }


    @PostMapping
    public String createUser(@RequestBody MyBatisUser user) {
        userService.createUser(user);
        return "User created successfully (MyBatis)";
    }

    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    @GetMapping("/map/{id}")
    public Map<String,Object> getUserMap(@PathVariable Long id) {
        return userService.getUserMapById(id);
    }

    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @PutMapping("/{id}")
    public String updateUser(@PathVariable Long id, @RequestBody MyBatisUser user) {
        user.setId(id);
        userService.updateUser(user);
        return "User updated successfully (MyBatis)";
    }

    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return "User deleted successfully (MyBatis)";
    }

    @GetMapping("/address/{id}")
    public MyBatisUser findByIdWithAddress(@PathVariable Long id){
        return userService.findByIdWithAddress(id);
    }
}
